package cc.orange.adapter;

import android.text.SpannableString;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cc.orange.R;
import cc.orange.entity.KFChatListEntity;
import cc.orange.utils.CircleImageView;
import cc.orange.utils.emojis.AndroidEmoji;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;


/**
 * Created by CM on 2022/10/25.
 */

public class MsgCon4KFAdapter extends BaseQuickAdapter<KFChatListEntity.Data, BaseViewHolder> {
    public MsgCon4KFAdapter(@Nullable List<KFChatListEntity.Data> data) {
        super(R.layout.rc_message_item, data);
    }

    private String name,photo,uid;
    public void setNamePhoto(String name,String photo,String uid){
        this.name = name;
        this.photo = photo;
        this.uid = uid;
    }
    @Override
    protected void convert(@NonNull BaseViewHolder helper, KFChatListEntity.Data item) {
        CircleImageView rc_left_portrait = helper.getView(R.id.rc_left_portrait);
        CircleImageView rc_right_portrait = helper.getView(R.id.rc_right_portrait);
        RelativeLayout rc_content = helper.getView(R.id.rc_content);//
        TextView  talk_msg_content = helper.getView(R.id.talk_msg_content); //消息文本
        TextView  rc_time = helper.getView(R.id.rc_time); //时间

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0,20,0,0);
        talk_msg_content.setText("");
        if(item.getClassify() == 1){//用户  1客服
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            rc_content.setBackgroundResource(R.drawable.shape_msg_left);
            talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.black));
            rc_left_portrait.setVisibility(View.VISIBLE);
            rc_right_portrait.setVisibility(View.GONE);
            Glide.with(mContext).load(item.getPhoto()).into(rc_left_portrait);
        }else{
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            rc_content.setBackgroundResource(R.drawable.shape_msg_right);
            talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.white));
            rc_left_portrait.setVisibility(View.GONE);
            rc_right_portrait.setVisibility(View.VISIBLE);
            ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
            String url_talk = ss.getString("url_talk", "");
            Glide.with(mContext).load(url_talk).into(rc_right_portrait);
        }
        rc_content.setLayoutParams(layoutParams);


        talk_msg_content.setVisibility(View.VISIBLE);
        String content = item.getText();
        content = content.replace("\n", " ");
        if (content.length() > 100) {
            content = content.substring(0, 100);
        }
        talk_msg_content.setText(new SpannableString(AndroidEmoji.ensure(content)));
    }
}
